package com.kinptm.masterclock.ui;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.kinptm.masterclock.Intents;
import com.kinptm.masterclock.R;
import com.kinptm.masterclock.bean.AlarmRang;
import com.kinptm.masterclock.utils.Ln;
import com.kinptm.masterclock.utils.ViewUtil;

import java.io.IOException;

public class AlarmRangActivity extends SherlockFragmentActivity {

    private AlarmRang mAlarmRang;
    private Vibrator mVibrator;
    private KeyguardManager.KeyguardLock mKeyguardLock;
    private PowerManager.WakeLock mWakeLock;
    private boolean isScreenLock;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_rang);

        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        isScreenLock = !pm.isScreenOn();
        if (isScreenLock) {
            mKeyguardLock = km.newKeyguardLock("unLock");
            mWakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
            mKeyguardLock.disableKeyguard();
            mWakeLock.acquire();
        }
        startAlarm(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        startAlarm(intent);
    }

    private void startAlarm(Intent intent) {
        mAlarmRang = (AlarmRang) intent.getSerializableExtra(Intents.ALARM.EXTRA_ALARM_RANG);
        if (mAlarmRang == null) {
            Ln.e("%s", "Alarm Clock is null !");
            return;
        }
        if (mAlarmRang.isVibrate()) {
            vibrator();
        } else {
            cancelVibrator();
        }
        if (mAlarmRang.isSound()) {
            sound(mAlarmRang);
        } else {
            cancelSound();
        }
        ViewUtil.findTextViewAndSetText(this, R.id.alarm_rang_title_tv, mAlarmRang.getTitle());
        ViewUtil.findTextViewAndSetText(this, R.id.alarm_rang_content_tv, mAlarmRang.getRemark());
    }

    private void vibrator() {
        if (mVibrator == null)
            mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        mVibrator.cancel();
        mVibrator.vibrate(new long[]{500, 500}, 0);
    }


    private void sound(AlarmRang alarmRang) {
        String uri = alarmRang.getSoundUri();
        if (TextUtils.isEmpty(uri))
            return;
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            float volume = alarmRang.getVolume() / 100f;
            mMediaPlayer.setVolume(volume, volume);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setLooping(true);
        }
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
        }
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(this, Uri.parse(uri));
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onS(View view) {

        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelSound();
        cancelVibrator();
        if (isScreenLock) {
            mKeyguardLock.reenableKeyguard();
            mWakeLock.release();
        }
        mWakeLock = null;
        mKeyguardLock = null;
    }

    private void cancelSound() {
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying())
                mMediaPlayer.stop();
            mMediaPlayer.release();
        }
    }

    private void cancelVibrator() {
        if (mVibrator != null)
            mVibrator.cancel();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
}
